package preprocess;

import java.io.*;
import java.util.*;

import utils.ReadWriteCritical;

/**
 * 数据接收发送控制类
 * 
 * @author yan
 * 
 */
public class XmlTransfer
{
    /**
     * 判断xmlControl函数是否曾经被执行过，如果曾经被执行过，则不会再次执行 @by huangcd
     */
    private static boolean          hasStarted   = false;
    /**
     * 每次发送完一篇XML文档以后，休眠指定时间再发送下一篇 @by huangcd
     */
    public static long              timeInterval = 1000;
    public static boolean           isFull       = false;                  // 管道是否满
    public static boolean           isNew        = false;                  // 是否读取新文件
    public static int               pipeSize     = 1024;                   // 管道容量
    public static int               length       = 0;                      // 文件长度
    public static boolean           isEnd        = false;                  // 所有文件是否已经读完
    public static String            xmlName1     = "";
    public static String            xmlName2     = "";
    public static ArrayList<String> xmlNameList  = new ArrayList<String>();
    public static ArrayList<byte[]> xmlList      = new ArrayList<byte[]>();
    public static int               size         = 5;
    public static ReadWriteCritical critical     = new ReadWriteCritical();
    private String                  getPath;
    private XmlGenerate             gene         = null;
    private XmlReceive              rece         = null;
    public static boolean           isGoOn       = false;
    public static String            listenDir    = "";

    public XmlTransfer(String path)
    {
        getPath = path;
        isFull = false;
        isNew = false;
        length = 0;
        isEnd = false;
        xmlName1 = "";
        xmlName2 = "";
        xmlNameList = new ArrayList<String>();
        xmlList = new ArrayList<byte[]>();
    }

    public void xmlControl()
    {
        if (hasStarted)
            return;
        try
        {
            hasStarted = true;
            gene = new XmlGenerate(getPath);
            rece = new XmlReceive(pipeSize);

            PipedOutputStream Sender = gene.getOutputStream();
            PipedInputStream Receiver = rece.getInputStream();
            Sender.connect(Receiver);

            gene.start();
            rece.start();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
